﻿using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

using TakePhotoTogether.ImageProcessing;
using System.Windows;

namespace TakePhotoTogetherTest
{
    [TestClass]
    public class CVImageTest
    {
        [TestMethod]
        public void Test_SaveAndLoadImageFromCameraRoll()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    CVImage image = new CVImage(100, 100);

                    image.SavePhoto();
                    string fileName = image.LastSavedName;

                    CVImage loadedImage = CVImage.LoadCVImage(fileName);

                    Assert.AreEqual(image.Width, loadedImage.Width, "Loaded image doesn't have same Width!");
                    Assert.AreEqual(image.Height, loadedImage.Height, "Loaded image doesn't have same Height!");
                });
        }

        [TestMethod]
        public void Test_ResizeImage()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                CVImage image = new CVImage(100, 100);

                int number_tests = 10000;

                Random rnd = new Random();

                for (int n = 0; n < number_tests; n++)
                {
                    int newHeight = rnd.Next(1,1000);
                    int newWidth = rnd.Next(1,1000);
                    int interpolationMethod = rnd.Next(0,2);

                    CVImage resizedImage = image.Resize(newHeight, newWidth, 
                        interpolationMethod == 0? Interpolation.Bilinear : Interpolation.NearestNeighbor);

                    Assert.AreEqual(newWidth, resizedImage.Width, "Resized image doesn't have the right Width!");
                    Assert.AreEqual(newHeight, resizedImage.Height, "Resized image doesn't have the right Height!");
                }
            });
        }

        [TestMethod]
        public void Test_ConcatImage()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                CVImage image = new CVImage(100, 100);

                int number_tests = 10000;

                Random rnd = new Random();

                for (int n = 0; n < number_tests; n++)
                {
                    int newHeight = rnd.Next(1, 1000);
                    int newWidth = rnd.Next(1, 1000);
                    int interpolationMethod = rnd.Next(0, 2);

                    CVImage resizedImage = image.Resize(newHeight, newWidth,
                        interpolationMethod == 0 ? Interpolation.Bilinear : Interpolation.NearestNeighbor);

                    CVImage concatImage = image.Concat(resizedImage);

                    Assert.AreEqual(newWidth+image.Width, concatImage.Width, "Concated image doesn't have the right Width!");
                    Assert.AreEqual(image.Height > newHeight? image.Height : newHeight, resizedImage.Height, "Resized image doesn't have the right Height!");
                }
            });
        }

        [TestMethod]
        public void Test_CropImage()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                CVImage image = new CVImage(100, 100);

                int number_tests = 10000;

                Random rnd = new Random();

                for (int n = 0; n < number_tests; n++)
                {
                    int newHeight = rnd.Next(1, image.Height);
                    int newWidth = rnd.Next(1, image.Width);
                    CVImage croppedImage = new CVImage(newWidth, newHeight);

                    PixelUtil.Crop(image.Width, image.Height, image.Pixels, newWidth, newHeight, 0, 0, croppedImage.Pixels);

                    Assert.AreEqual(newWidth, croppedImage.Width, "Cropped image doesn't have the right Width!");
                    Assert.AreEqual(newHeight, croppedImage.Height, "Resized image doesn't have the right Height!");
                }
            });
        }
    }
}
